package com.ejlchina.tool.fragment

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import com.adolf.androiddemo.databinding.DialogFirmwareUpgradeBinding
import com.ejlchina.tool.adapter.FirmwareUpgradeDialogAdapter
import com.ejlchina.tool.base.BaseDialogFragment
import com.ejlchina.tool.bean.BleDeviceBean
import com.ejlchina.tool.singleton.GlobalAction
import com.ejlchina.tool.singleton.GlobalConnectObject
import com.ejlchina.tool.singleton.GlobalParams
import com.ejlchina.tool.util.LOG_TAG
import com.ejlchina.tool.util.removeSpecifiedDeviceInList
import com.ejlchina.tool.viewmodel.MainViewModel
import kotlinx.coroutines.launch

class FirmwareUpgradeDialogFragment: BaseDialogFragment() {
    private lateinit var binding: DialogFirmwareUpgradeBinding
    private lateinit var firmwareUpgradeDialogAdapter: FirmwareUpgradeDialogAdapter
    protected val viewModelActivity : MainViewModel by activityViewModels()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        binding = DialogFirmwareUpgradeBinding.inflate(inflater,container,false)
        return binding.root

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initListeners()
        initData()
        initObservers()
    }

    private fun initData() {
        firmwareUpgradeDialogAdapter = FirmwareUpgradeDialogAdapter(GlobalParams.firmwareUpgradeList)
        initProgressAdapterData()
    }

    private fun initListeners(){
        binding.ivClose.setOnClickListener {
            dismiss()
        }
    }

    private fun initObservers(){
        viewLifecycleOwner.lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED){
                launch {
                    Log.i(LOG_TAG,"$777777-----${GlobalAction.firmwareUpgradeMap.size}")
                    for (item in GlobalAction.firmwareUpgradeMap.keys){
                        launch {
                            GlobalAction.firmwareUpgradeMap[item]?.collect{
                                firmwareUpgradeDialogAdapter.notifyDataSetChanged()
                            }
                        }
                    }
                }
            }
        }
    }

    private fun initProgressAdapterData(){
        binding.rvFirmwareData.apply {
            adapter = firmwareUpgradeDialogAdapter
            layoutManager = LinearLayoutManager(requireContext())
        }
    }

    override fun onResume() {
        super.onResume()
        GlobalParams.firmwareUpgradeList.removeIf { localConnectBean ->
            !viewModelActivity.getAllConnectDevices().map { it.mac }.contains(localConnectBean.bleDevice.mac)
        }
        firmwareUpgradeDialogAdapter.notifyDataSetChanged()
        if (GlobalParams.firmwareUpgradeList.size == 0){
            dismiss()
        }
    }
}